<?php

class Tourism_goods_category_model extends CI_Model
{
    private $table = 'tourism_goods_category';


    //根据id获取一条类别信息
    public function findById($id)
    {
        $this->db->select('*');
        $this->db->from($this->table);
        $this->db->where('id', $id);
        $this->db->limit(1);
        return $this->db->get();
    }

     /**
     * 查找子元素
     * @param unknown $id
     */
    public function findChildren($id){
        
        $id = intval($id);
        $this->db->select('id,name,parent_id,status,level');
        $this->db->from($this->table);
        $this->db->where('parent_id',$id);
        $this->db->where('status',1);
        return $this->db->get();
    }
    
    /**
     *  类型--父级元素
     * @param unknown $level
     * @param unknown $parent_id
     */
    public function findPrevCategory($level,$parent_id){
    	
        $this->db->select('id,name,parent_id,status,level');
        $this->db->from($this->table);
        $this->db->where('parent_id',$parent_id);
        $this->db->where('status',1);
        return $this->db->get();
    }
    
	public function findParents($id){
	    
	    $id = intval($id);
        $this->db->select('tourism_goods_category.*');
        $this->db->from($this->table.' AS tourism_goods_category');
        $this->db->join("(SELECT parent_id FROM tourism_goods_category WHERE id = {$id} ) AS alias", 'alias.parent_id=tourism_goods_category.parent_id','inner');
        return $this->db->get();
    }

    //获取其子类的ids
    public function find($getData)
    {
        $this->db->select('*');
        $this->db->from($this->table);
        if(!empty($getData['level'])){
            $this->db->where('level', $getData['level']);
        }
        if(!empty($getData['parent_id'])){
            $this->db->where('parent_id', $getData['parent_id']);
        }
        return $this->db->get();
    }
    
    /**
     * 获取3子元素
     * @param unknown $cid
     * @return multitype:NULL
     */
    public function getCategoryChildrenIds($cid)
    {
        $params = array();
        $cid = ',' . $cid . ',';
        $this->db->select('tourism_goods_category.id');
        $this->db->where('tourism_goods_category.level', 3);
        $this->db->like('tourism_goods_category.parents', $cid);
        $result = $this->db->get($this->table);
        if ($result->num_rows() > 0) {
            foreach ($result->result() as $item) {
                $params[] = $item->id;
            }
        }
        return $params;
    }
}